import { Injectable } from '@angular/core';
import { HttpClient } from '@angular/common/http';

import { Observable } from 'rxjs/Observable';
import { catchError } from 'rxjs/operators';

const BaseURL = '/platform/api/users';

@Injectable()
export class UsersService {

  constructor(private http: HttpClient) { }

  getUsers(token: string) {
    return this.http.get(BaseURL, {
      params: {
        access_token: token
      }
    }).pipe(
      catchError(error => Observable.throw(error))
    );
  }

  deleteUser(id: string, token: string) {
    return this.http.delete(`${BaseURL}/${id}`, {
      params: {
        access_token: token
      }
    }).pipe(
      catchError(error => Observable.throw(error))
    );
  }

  newUser(data, token: string) {
    return this.http.post(BaseURL, data, {
      params: {
        access_token: token
      }
    }).pipe(
      catchError(error => Observable.throw(error))
    );
  }

  newAdmin(data, token: string) {
    return this.http.post(`${BaseURL}/administrator`, data, {
      params: {
        access_token: token
      }
    }).pipe(
      catchError(error => Observable.throw(error))
    );
  }

  editUser(id: string, data, token: string) {
    return this.http.patch(`${BaseURL}/${id}`, data, {
      params: {
        access_token: token
      }
    }).pipe(
      catchError(error => Observable.throw(error))
    );
  }

}
